<?php

namespace app\api\controller;

use app\common\controller\Api;

/**
 * 用户访问记录接口
 */
class UserAccessLog extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function addUserAccessLog()
    {
        if ($this->request->isPost()) {
            if (!$this->auth) {
                $this->success();
            }
            $userId = $this->auth->id;
            $row = model('\app\admin\model\jy\UserAccessLog')->where(['user_id' => $userId, 'create_time' => ['>=', date("Y-m-d", time())]])->find();
            if ($row) {
                $row->isUpdate(true)->save(['update_time' => date("Y-m-d H:i:s", time())]);
                $this->success();
            } else {
                model('\app\admin\model\jy\UserAccessLog')->insert(['user_id' => $userId, 'update_time' => date("Y-m-d H:i:s", time())]);
            }
        }
        $this->success();
    }

    public function delUserAccessLog($id = NULL)
    {
        $row = model('\app\admin\model\jy\UserAccessLog')->get($id);
        if (!$row) {
            $this->error('记录不存在');
        }
        $row->delete();
        $this->success();
    }

    public function getUserAccessLogList()
    {
        $pageIndex = $this->request->post('pageIndex', 1);
        $pageSize = $this->request->post('pageSize', 10);
        $user = $this->auth->getUser();
        // 超级管理员
        if ($user['group_id'] == 2) {
            $this->error(__('You have no permission'));
        }
        //当前是否为关联查询
        $this->relationSearch = true;
        $list = model('\app\admin\model\jy\UserAccessLog')
            ->field('ua.id,ua.update_time,u.nickname,u.avatar,ui.user_id, u1.id as invite_user_id, u1.nickname as invite_nickname,u1.avatar as invite_avatar,ui.invite_type')
            ->alias('ua')
            ->join('jy_user_invite ui', 'ua.user_id=ui.user_id', 'LEFT')
            ->join('jy_user u', 'ua.user_id=u.id', 'INNER')
            ->join('jy_user u1', 'ui.invite_user_id=u1.id', 'LEFT')
            ->order('ua.update_time desc')
            ->paginate($pageSize, false, ['page' => $pageIndex]);

        $newList = [];
        if ($list) {
            foreach ($list as $item) {
                $newList[] = [
                    'id'              => $item['id'],
                    'date'            => date('Y年m月d日', strtotime($item['update_time'])),
                    'time'            => date('G:i', strtotime($item['update_time'])),
                    'avatar'          => cdnurl($item['avatar']),
                    'nickname'        => $item['nickname'],
                    'invite_nickname' => $item['invite_nickname'] ? $item['invite_nickname'] : '无',
                    'invite_type'     => $item['invite_type'] ? $item['invite_type'] : '0'
                ];
            }
        }
//        $newList = [
//            [
//                "id"              => 2,
//                "date"            => "2021-03-31",
//                "time"            => "22:11",
//                "avatar"          => "http://192.168.1.124/uploads/20210311/d4b66887179b86205356b36a7220de35.png",
//                "nickname"        => "admin",
//                "invite_nickname" => "22222222",
//                "invite_type"     => "1"
//            ],
//            [
//                "id"              => 3,
//                "date"            => "2021-03-10",
//                "time"            => "10:52",
//                "avatar"          => "http://192.168.1.124/uploads/20210305/594aea548d2f6692485e615ca253df8e.jpg",
//                "nickname"        => "22222222",
//                "invite_nickname" => "admin",
//                "invite_type"     => "1"
//            ],
//            [
//                "id"              => 3,
//                "date"            => "2021-03-31",
//                "time"            => "22:11",
//                "avatar"          => "http://192.168.1.124/uploads/20210311/d4b66887179b86205356b36a7220de35.png",
//                "nickname"        => "admin",
//                "invite_nickname" => "22222222",
//                "invite_type"     => "1"
//            ],
//            [
//                "id"              => 3,
//                "date"            => "2021-03-10",
//                "time"            => "10:52",
//                "avatar"          => "http://192.168.1.124/uploads/20210305/594aea548d2f6692485e615ca253df8e.jpg",
//                "nickname"        => "22222222",
//                "invite_nickname" => "admin",
//                "invite_type"     => "1"
//            ],
//            [
//                "id"              => 3,
//                "date"            => "2021-03-31",
//                "time"            => "22:11",
//                "avatar"          => "http://192.168.1.124/uploads/20210311/d4b66887179b86205356b36a7220de35.png",
//                "nickname"        => "admin",
//                "invite_nickname" => "22222222",
//                "invite_type"     => "1"
//            ],
//            [
//                "id"              => 3,
//                "date"            => "2021-03-10",
//                "time"            => "10:52",
//                "avatar"          => "http://192.168.1.124/uploads/20210305/594aea548d2f6692485e615ca253df8e.jpg",
//                "nickname"        => "22222222",
//                "invite_nickname" => "admin",
//                "invite_type"     => "1"
//            ]
//        ];
        $result = array("total" => $list->total(), "rows" => $newList);
        $this->success('', $result);
    }
}